package cn.edu.suse.epilepsy.service.impl;


import cn.edu.suse.epilepsy.entity.dto.DoctorAddDto;
import cn.edu.suse.epilepsy.entity.pojo.DoctorManage;
import cn.edu.suse.epilepsy.entity.vo.DoctorMannageVo;
import cn.edu.suse.epilepsy.mapper.DoctorManageMapper;
import cn.edu.suse.epilepsy.service.DoctorManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: QingShu
 * @Date: 2024/7/7 22:45
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class DoctorManageServiceImpl extends ServiceImpl<DoctorManageMapper, DoctorManage> implements DoctorManageService {
    @Autowired
    private DoctorManageMapper doctorManageMapper;
    @Override
    public DoctorManage addDoctor(DoctorAddDto dto) {
        DoctorManage doctorManage = new DoctorManage();
        doctorManage.setStatus("已启用");
        doctorManage.setDate(new Date());
        BeanUtils.copyProperties(dto, doctorManage);
        doctorManageMapper.insert(doctorManage);
        return doctorManage;
    }
//    public Page<DoctorMannageVo> page(Page<DoctorMannageVo> page) {
//        return this.page(page);
//    }

    @Override
    public List<DoctorMannageVo> selectAllDoctor() {
        List<DoctorManage> doctorManages = doctorManageMapper.selectList(null);
        List<DoctorMannageVo> doctorMannageVos = new ArrayList<>();

        for (DoctorManage doctorManage : doctorManages) {
            DoctorMannageVo doctorMannageVo = new DoctorMannageVo();
            BeanUtils.copyProperties(doctorManage, doctorMannageVo);
            doctorMannageVos.add(doctorMannageVo);
        }

        return doctorMannageVos;

    }

    @Override
    public int updateDoctor(DoctorManage doctorManage) {
        int i = doctorManageMapper.updateById(doctorManage);
        return i;
    }
    @Override
    public List<DoctorManage> searchDoctors(String name, String hospital, String status, String department) {
            QueryWrapper<DoctorManage> queryWrapper = new QueryWrapper<>();

            // 添加姓名模糊查询条件，如果name不为空
            if (StringUtils.isNotBlank(name)) {
                queryWrapper.like("doc_info_name", name);
            }

            // 添加医院模糊查询条件，如果hospital不为空
            if (StringUtils.isNotBlank(hospital)) {
                System.out.println(hospital);
                queryWrapper.like("doc_info_hospital", hospital);
            }

            // 添加状态模糊查询条件，如果status不为空
            if (StringUtils.isNotBlank(status)) {
                queryWrapper.like("doc_info_status", status);
            }

            // 添加科室模糊查询条件，如果department不为空
            if (StringUtils.isNotBlank(department)) {
                queryWrapper.like("doc_info_dep", department);
            }

            return doctorManageMapper.selectList(queryWrapper);

    }

//   @Override
//        public List<DoctorTradeInfo> selectDoctorTradeInfoByDoctorId(Integer doctorId) {
//            QueryWrapper<DoctorTradeInfo> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("doctor_id", doctorId);
//            return doctorManageMapper.selectList(queryWrapper);
//        }

}
